<template>
  <div class="login">
    <div class="login-box">
      <div class="form-header">
        <p>登录</p>
        <el-link type="success" @click="toURL">注册账号</el-link>
      </div>
      <el-form :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
        <el-form-item label="" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名">
          </el-input>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="">
          <el-button type="primary" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
      <p class="tips"><span>账号:admin</span><span>密码:admin</span></p>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/login";
import Cookie from "js-cookie";
import { mapMutations } from "vuex";
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "focus" },
          {
            min: 1,
            max: 10,
            message: "长度在 1 到 10 个字符",
            trigger: "focus",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "focus" },
          {
            min: 5,
            max: 12,
            message: "长度在 5 到 12 个字符",
            trigger: "focus",
          },
        ],
      },
    };
  },
  mounted() {},
  created() {},
  methods: {
    ...mapMutations(["setUserInfo"]),
    toURL() {
      this.$router.push({ path: "/register" });
    },
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        const res = await login(this.loginForm);
        if (res.status === 0) {
          this.setUserInfo(res.data.userinfo);
          Cookie.set("token", res.data.token);
          this.$message.success(res.message);
          this.$router.push("/");
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  background-image: -moz-linear-gradient(
    45deg,
    rgb(168, 200, 255),
    rgb(191, 255, 189)
  );

  background-image: -webkit-linear-gradient(
    45deg,
    rgb(168, 200, 255),
    rgb(191, 255, 189)
  );

  background-image: linear-gradient(
    45deg,
    rgb(168, 200, 255),
    rgb(191, 255, 189)
  );

  .form-header {
    display: flex;
    justify-content: space-between;
    padding: 20px 0;
    margin: 0 5px;

    p {
      margin: 0;
      font-weight: 600;
      font-size: 25px;
      color: #333;
    }
  }

  .login-box {
    position: absolute;
    width: 300px;
    height: 280px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 50px;
    border-radius: 20px;
    background: #fff;

    .el-form {
      .el-input,
      .el-button {
        width: 300px;
      }
    }

    .tips {
      font-size: 14px;

      span {
        margin-right: 15px;
      }
    }
  }
}
</style>
